package com.gxgeek.javabasic.base;

import java.net.*;
import java.util.Enumeration;

/**
 * Created by Administrator on 2017/7/9.
 */
public class Test {
    public static void main(String[] args) throws UnknownHostException {
//        Long l = new Long(123);
//        Integer i = 123;
//        System.out.println(i.equals(l));
//        System.out.println(l.equals(i));
        System.out.println(getLocalIpByNetcard());
//        System.out.println(InetAddress.getLocalHost().getHostAddress());
    }

    public static String getLocalIpByNetcard() {
        try {
            for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
                NetworkInterface item = e.nextElement();
                for (InterfaceAddress address : item.getInterfaceAddresses()) {
                    if (item.isLoopback() || !item.isUp()) {
                        continue;
                    }
                    if (address.getAddress() instanceof Inet4Address) {
                        Inet4Address inet4Address = (Inet4Address) address.getAddress();
                        return inet4Address.getHostAddress();
                    }
                }
            }
            return InetAddress.getLocalHost().getHostAddress();
        } catch (SocketException | UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

}
